package com.dianping.cat.home.user.transform;

import com.dianping.cat.home.user.IEntity;
import com.dianping.cat.home.user.IVisitor;
import com.dianping.cat.home.user.entity.User;
import com.dianping.cat.home.user.entity.UserConfig;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;









public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  27 */   public DefaultXmlBuilder() { this(false); }



  public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  35 */     this.m_compact = compact;
    this.m_sb = sb;
/*  37 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  41 */     entity.accept(this.m_visitor);
/*  42 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  46 */     this.m_level--;

/*  48 */     indent();
/*  49 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  53 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  57 */     if (value == null) {
/*  58 */       return null;
    }

/*  61 */     String str = toString(value);
/*  62 */     int len = str.length();
/*  63 */     StringBuilder sb = new StringBuilder(len + 16);

/*  65 */     for (int i = 0; i < len; i++) {
/*  66 */       char ch = str.charAt(i);

/*  68 */       switch (ch) {
        case '<':
/*  70 */           sb.append("&lt;");
          break;
        case '>':
/*  73 */           sb.append("&gt;");
          break;
        case '&':
/*  76 */           sb.append("&amp;");
          break;
        case '"':
/*  79 */           if (!text) {
/*  80 */             sb.append("&quot;");
            break;
          }
        default:
/*  84 */           sb.append(ch);
          break;
      }

    }
/*  89 */     return sb.toString();
  }

  protected void indent() {
/*  93 */     if (!this.m_compact) {
/*  94 */       for (int i = this.m_level - 1; i >= 0; i--) {
        this.m_sb.append("   ");
      }
    }
  }


/* 101 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 105 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 109 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 113 */     indent();

/* 115 */     this.m_sb.append('<').append(name);

/* 117 */     int len = nameValues.length;

/* 119 */     for (int i = 0; i + 1 < len; i += 2) {
/* 120 */       Object attrName = nameValues[i];
/* 121 */       Object attrValue = nameValues[i + 1];

/* 123 */       if (attrValue != null) {
/* 124 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 128 */     if (dynamicAttributes != null) {
/* 129 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 130 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 134 */     if (text != null && closed) {
/* 135 */       this.m_sb.append('>');
/* 136 */       this.m_sb.append(escape(text, true));
/* 137 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 139 */       if (closed) {
/* 140 */         this.m_sb.append('/');
      } else {
/* 142 */         this.m_level++;
      }

/* 145 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 151 */     if (value instanceof String)
/* 152 */       return (String)value; 
/* 153 */     if (value instanceof Collection) {
/* 154 */       Collection<Object> list = (Collection)value;
/* 155 */       StringBuilder sb = new StringBuilder(32);
/* 156 */       boolean first = true;

/* 158 */       for (Object item : list) {
/* 159 */         if (first) {
/* 160 */           first = false;
        } else {
/* 162 */           sb.append(',');
        }

/* 165 */         if (item != null) {
/* 166 */           sb.append(item);
        }
      }

/* 170 */       return sb.toString();
/* 171 */     }  if (value.getClass().isArray()) {
/* 172 */       int len = Array.getLength(value);
/* 173 */       StringBuilder sb = new StringBuilder(32);
/* 174 */       boolean first = true;

/* 176 */       for (int i = 0; i < len; i++) {
/* 177 */         Object item = Array.get(value, i);

/* 179 */         if (first) {
/* 180 */           first = false;
        } else {
/* 182 */           sb.append(',');
        }

/* 185 */         if (item != null) {
/* 186 */           sb.append(item);
        }
      }

/* 190 */       return sb.toString();
    }

/* 193 */     return String.valueOf(value);
  }



/* 198 */   public void visitUser(User user) { startTag("user", true, null, new Object[] { "id", user.getId(), "role", Integer.valueOf(user.getRole()) }); }



  public void visitUserConfig(UserConfig userConfig) {
/* 203 */     startTag("user-config", null, new Object[0]);

/* 205 */     if (!userConfig.getUsers().isEmpty()) {
/* 206 */       for (User user : userConfig.getUsers().values()) {
/* 207 */         user.accept(this.m_visitor);
      }
    }

/* 211 */     endTag("user-config");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/user/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */